package dao.impl;

import dao.BaseDao;
import dao.UserDao;
import domain.User;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author HLF
 * @version V1.0
 * @package dao.impl
 * @date 2021/10/30 19:21
 */
public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public List<User> selectAllUsers() {
        Connection conn = null;
        List<User> users = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name,password from user;";
            users = queryForMore(conn, sql, User.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return users;
    }

    @Override
    public User selectUserByIdAndPassword(User user) {
        Connection conn = null;
        User user2 = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name, password from user where id = ? and password = ? ;";
            user2 = queryForOne(conn, sql, User.class,user.getId(),user.getPassword());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return user2;
    }

    @Override
    public User selectUserById(Integer id) {
        Connection conn = null;
        User user2 = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name ,password from user where id = ? ;";
            user2 = queryForOne(conn, sql, User.class,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return user2;
    }

    @Override
    public int insertUser(User user) {
        Connection conn = null;
        int insert = 0;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "insert into user values(?,?,?);";
            insert = update(conn, sql,user.getId(),user.getName(),user.getPassword());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return insert;
    }

    @Override
    public int updateUser(User user) {
        Connection conn = null;
        int update = 0;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "update user set id = ?,name = ?,password = ? where id = ?;";
            update = update(conn, sql,user.getId(),user.getName(),user.getPassword(),user.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return update;
    }

    @Override
    public int deleteUserById(Integer id) {
        Connection conn = null;
        int delete = 0;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "delete from user where id = ?;";
            delete = update(conn, sql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.closeResource(conn,null);
        }
        return delete;
    }
}
